package org.jetnuke.modules.admin.authors;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.jforum.dao.DataAccessDriver;
import net.jforum.dao.security.GroupSecurityDAO;
import net.jforum.repository.SecurityRepository;
import net.jforum.security.Role;
import net.jforum.security.RoleCollection;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.common.SecurityConstants;
import org.jetnuke.util.JetUtil;


public class AuthorUpdate implements IAction {
	int aid;

	int add_radminarticle, add_radmintopic, add_radminuser, add_radminsurvey,
			add_radminsection, add_radminlink, add_radminephem, add_radminfaq,
			add_radmindownload, add_radminreviews, add_radminnewsletter,
			add_radmincontent, add_radminency,
			add_radminsuper;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.common.IAction#exec(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse,
	 *      org.apache.velocity.VelocityContext, org.jetnuke.common.IModuleInfo,
	 *      java.lang.String, int)
	 */
	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {

		if (ai == null || !ai.IsSuperUser())
			return null;//Error;
		RoleCollection rc=DataAccessDriver.getInstance().newGroupSecurityDAO().loadRoles(3);
		GroupSecurityDAO usm=DataAccessDriver.getInstance().newGroupSecurityDAO();
		
		Role role=rc.get(SecurityConstants.AdminArticle);
		
		if(add_radminarticle==1)
			usm.addRole(aid,role);
		else
			usm.deleteRole(aid,role.getName());
		
		role=rc.get(SecurityConstants.AdminTopic);
		if(add_radmintopic==1)
			usm.addRole(aid,role);
		else
			usm.deleteRole(aid,role.getName());

		role=rc.get(SecurityConstants.AdminUser);
		if(add_radminuser==1)
			usm.addRole(aid,role);
		else
			usm.deleteRole(aid,role.getName());

		role=rc.get(SecurityConstants.AdminSurvey);
		if(add_radminsurvey==1)
			usm.addRole(aid,role);
		else
			usm.deleteRole(aid,role.getName());

		role=rc.get(SecurityConstants.AdminSection);
		if(add_radminsection==1)
			usm.addRole(aid,role);
		else
			usm.deleteRole(aid,role.getName());

		role=rc.get(SecurityConstants.AdminLink);
		if(add_radminlink==1)
			usm.addRole(aid,role);
		else
			usm.deleteRole(aid,role.getName());

		role=rc.get(SecurityConstants.AdminEphem);
		if(add_radminephem==1)
			usm.addRole(aid,role);
		else
			usm.deleteRole(aid,role.getName());

		role=rc.get(SecurityConstants.AdminFaq);
		if(add_radminfaq==1)
			usm.addRole(aid,role);
		else
			usm.deleteRole(aid,role.getName());
		
		role=rc.get(SecurityConstants.AdminDownload);
		if(add_radmindownload==1)
			usm.addRole(aid,role);
		else
			usm.deleteRole(aid,role.getName());
		
		role=rc.get(SecurityConstants.AdminReviews);
		if(add_radminreviews==1)
			usm.addRole(aid,role);
		else
			usm.deleteRole(aid,role.getName());
		
		role=rc.get(SecurityConstants.AdminNewsLetter);
		if(add_radminnewsletter==1)
			usm.addRole(aid,role);
		else
			usm.deleteRole(aid,role.getName());
		
		role=rc.get(SecurityConstants.AdminContent);
		if(add_radmincontent==1)
			usm.addRole(aid,role);
		else
			usm.deleteRole(aid,role.getName());
		
		role=rc.get(SecurityConstants.AdminEncy);
		if(add_radminency==1)
			usm.addRole(aid,role);
		else
			usm.deleteRole(aid,role.getName());
		
		rc=DataAccessDriver.getInstance().newGroupSecurityDAO().loadRoles(2);
		role=rc.get(net.jforum.security.SecurityConstants.PERM_ADMINISTRATION);
		if(add_radminsuper==1)
			usm.addRole(aid,role);
		else if (aid!=2)
			usm.deleteRole(aid,role.getName());
		
		SecurityRepository.clean();
		
		
		
		
	/*	List ls = DataAccessDriver.getInst().newAuthorsModel().getByAID(aid);
				
		Author au = (Author) ls.get(0);

		au.setEMAIL(email);
		//au.setNAME(name);
		if (password.equals(password1)) {
			MessageDigest md = null;
			md = MessageDigest.getInstance("SHA-1"); //step 2
			md.update(password.getBytes("UTF-8")); //step 3
			byte raw[] = md.digest(); //step 4
			String crypted = (new BASE64Encoder()).encode(raw); //step 5

			au.setPWD(crypted);
		}
		au.setRADMINARTICLE(add_radminarticle);
		au.setRADMINTOPIC(add_radmintopic);
		au.setRADMINCONTENT(add_radmincontent);
		au.setRADMINDOWNLOAD(add_radmindownload);
		au.setRADMINENCY(add_radminency);
		au.setRADMINEPHEM(add_radminephem);
		au.setRADMINFAQ(add_radminfaq);
		au.setRADMINFORUM(add_radminforum);
		au.setRADMINLINK(add_radminlink);
		au.setRADMINNEWSLETTER(add_radminnewsletter);
		au.setRADMINREVIEWS(add_radminreviews);
		au.setRADMINSECTION(add_radminsection);
		au.setRADMINSUPER(add_radminsuper);
		au.setRADMINSURVEY(add_radminsurvey);
		au.setRADMINUSER(add_radminuser);
		au.setADMLANGUAGE("");
		au.setURL(url);

		DataAccessDriver.getInst().newAuthorsModel().update(au);*/
		JetUtil.redirect(vc,response,request.getContextPath()+"/do/admin/authors");
		return null;
	}

	public void setAdd_radminarticle(int add_radminarticle) {
		this.add_radminarticle = add_radminarticle;
	}

	public void setAdd_radmincontent(int add_radmincontent) {
		this.add_radmincontent = add_radmincontent;
	}

	public void setAdd_radmindownload(int add_radmindownload) {
		this.add_radmindownload = add_radmindownload;
	}

	public void setAdd_radminency(int add_radminency) {
		this.add_radminency = add_radminency;
	}

	public void setAdd_radminephem(int add_radminephem) {
		this.add_radminephem = add_radminephem;
	}

	public void setAdd_radminfaq(int add_radminfaq) {
		this.add_radminfaq = add_radminfaq;
	}

	public void setAdd_radminlink(int add_radminlink) {
		this.add_radminlink = add_radminlink;
	}

	public void setAdd_radminnewsletter(int add_radminnewsletter) {
		this.add_radminnewsletter = add_radminnewsletter;
	}

	public void setAdd_radminreviews(int add_radminreviews) {
		this.add_radminreviews = add_radminreviews;
	}

	public void setAdd_radminsection(int add_radminsection) {
		this.add_radminsection = add_radminsection;
	}

	public void setAdd_radminsuper(int add_radminsuper) {
		this.add_radminsuper = add_radminsuper;
	}

	public void setAdd_radminsurvey(int add_radminsurvey) {
		this.add_radminsurvey = add_radminsurvey;
	}

	public void setAdd_radmintopic(int add_radmintopic) {
		this.add_radmintopic = add_radmintopic;
	}

	public void setAdd_radminuser(int add_radminuser) {
		this.add_radminuser = add_radminuser;
	}

	public void setAid(int aid) {
		this.aid = aid;
	}

}
